package model

import (
	"database/sql"
	"strings"
	"time"
)

// JobGroup 执行器
type XxlJobGroup struct {
	Id          int       `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
	AppName     string    `gorm:"column:app_name;type:varchar(64);comment:执行器AppName;NOT NULL" json:"app_name"`
	Title       string    `gorm:"column:title;type:varchar(12);comment:执行器名称;NOT NULL" json:"title"`
	AddressType int       `gorm:"column:address_type;type:tinyint(4);default:0;comment:执行器地址类型：0=自动注册、1=手动录入;NOT NULL" json:"address_type"`
	AddressList string    `gorm:"column:address_list;type:text;comment:执行器地址列表，多地址逗号分隔" json:"address_list"`
	UpdateTime  time.Time `gorm:"column:update_time;type:datetime" json:"update_time"`
}

func (m *XxlJobGroup) TableName() string {
	return "xxl_job_group"
}

func (m *XxlJobGroup) GetRegistryList() []string {
	if m.AddressList != "" {
		return strings.Split(m.AddressList, ",")
	}

	return []string{}
}

type XxlJobInfo struct {
	Id                     int       `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
	JobGroup               int       `gorm:"column:job_group;type:int(11);comment:执行器主键ID;NOT NULL" json:"job_group"`
	JobDesc                string    `gorm:"column:job_desc;type:varchar(255);NOT NULL" json:"job_desc"`
	AddTime                time.Time `gorm:"column:add_time;type:datetime" json:"add_time"`
	UpdateTime             time.Time `gorm:"column:update_time;type:datetime" json:"update_time"`
	Author                 string    `gorm:"column:author;type:varchar(64);comment:作者" json:"author"`
	AlarmEmail             string    `gorm:"column:alarm_email;type:varchar(255);comment:报警邮件" json:"alarm_email"`
	ScheduleType           string    `gorm:"column:schedule_type;type:varchar(50);default:NONE;comment:调度类型;NOT NULL" json:"schedule_type"`
	ScheduleConf           string    `gorm:"column:schedule_conf;type:varchar(128);comment:调度配置，值含义取决于调度类型" json:"schedule_conf"`
	MisfireStrategy        string    `gorm:"column:misfire_strategy;type:varchar(50);default:DO_NOTHING;comment:调度过期策略;NOT NULL" json:"misfire_strategy"`
	ExecutorRouteStrategy  string    `gorm:"column:executor_route_strategy;type:varchar(50);comment:执行器路由策略" json:"executor_route_strategy"`
	ExecutorHandler        string    `gorm:"column:executor_handler;type:varchar(255);comment:执行器任务handler" json:"executor_handler"`
	ExecutorParam          string    `gorm:"column:executor_param;type:varchar(512);comment:执行器任务参数" json:"executor_param"`
	ExecutorBlockStrategy  string    `gorm:"column:executor_block_strategy;type:varchar(50);comment:阻塞处理策略" json:"executor_block_strategy"`
	ExecutorTimeout        int       `gorm:"column:executor_timeout;type:int(11);default:0;comment:任务执行超时时间，单位秒;NOT NULL" json:"executor_timeout"`
	ExecutorFailRetryCount int       `gorm:"column:executor_fail_retry_count;type:int(11);default:0;comment:失败重试次数;NOT NULL" json:"executor_fail_retry_count"`
	GlueType               string    `gorm:"column:glue_type;type:varchar(50);comment:GLUE类型;NOT NULL" json:"glue_type"`
	GlueSource             string    `gorm:"column:glue_source;type:mediumtext;comment:GLUE源代码" json:"glue_source"`
	GlueRemark             string    `gorm:"column:glue_remark;type:varchar(128);comment:GLUE备注" json:"glue_remark"`
	GlueUpdatetime         time.Time `gorm:"column:glue_updatetime;type:datetime;comment:GLUE更新时间" json:"glue_updatetime"`
	ChildJobid             string    `gorm:"column:child_jobid;type:varchar(255);comment:子任务ID，多个逗号分隔" json:"child_jobid"`
	TriggerStatus          int       `gorm:"column:trigger_status;type:tinyint(4);default:0;comment:调度状态：0-停止，1-运行;NOT NULL" json:"trigger_status"`
	TriggerLastTime        int64     `gorm:"column:trigger_last_time;type:bigint(20);default:0;comment:上次调度时间;NOT NULL" json:"trigger_last_time"`
	TriggerNextTime        int64     `gorm:"column:trigger_next_time;type:bigint(20);default:0;comment:下次调度时间;NOT NULL" json:"trigger_next_time"`
}

func (m *XxlJobInfo) TableName() string {
	return "xxl_job_info"
}

type XxlJobLock struct {
	LockName string `gorm:"column:lock_name;type:varchar(50);primary_key;comment:锁名称" json:"lock_name"`
}

func (m *XxlJobLock) TableName() string {
	return "xxl_job_lock"
}

type XxlJobLog struct {
	Id                     int          `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`
	JobGroup               int          `gorm:"column:job_group;type:int(11);comment:执行器主键ID;NOT NULL" json:"job_group"`
	JobId                  int          `gorm:"column:job_id;type:int(11);comment:任务，主键ID;NOT NULL" json:"job_id"`
	ExecutorAddress        string       `gorm:"column:executor_address;type:varchar(255);comment:执行器地址，本次执行的地址" json:"executor_address"`
	ExecutorHandler        string       `gorm:"column:executor_handler;type:varchar(255);comment:执行器任务handler" json:"executor_handler"`
	ExecutorParam          string       `gorm:"column:executor_param;type:varchar(512);comment:执行器任务参数" json:"executor_param"`
	ExecutorShardingParam  string       `gorm:"column:executor_sharding_param;type:varchar(20);comment:执行器任务分片参数，格式如 1/2" json:"executor_sharding_param"`
	ExecutorFailRetryCount int          `gorm:"column:executor_fail_retry_count;type:int(11);default:0;comment:失败重试次数;NOT NULL" json:"executor_fail_retry_count"`
	TriggerTime            sql.NullTime `gorm:"column:trigger_time;type:datetime;comment:调度-时间" json:"trigger_time"`
	TriggerCode            int          `gorm:"column:trigger_code;type:int(11);comment:调度-结果;NOT NULL" json:"trigger_code"`
	TriggerMsg             string       `gorm:"column:trigger_msg;type:text;comment:调度-日志" json:"trigger_msg"`
	HandleTime             sql.NullTime `gorm:"column:handle_time;type:datetime;comment:执行-时间" json:"handle_time"`
	HandleCode             int          `gorm:"column:handle_code;type:int(11);comment:执行-状态;NOT NULL" json:"handle_code"`
	HandleMsg              string       `gorm:"column:handle_msg;type:text;comment:执行-日志" json:"handle_msg"`
	AlarmStatus            int          `gorm:"column:alarm_status;type:tinyint(4);default:0;comment:告警状态：0-默认、1-无需告警、2-告警成功、3-告警失败;NOT NULL" json:"alarm_status"`
}

func (m *XxlJobLog) TableName() string {
	return "xxl_job_log"
}

type XxlJobLogReport struct {
	Id           int       `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
	TriggerDay   time.Time `gorm:"column:trigger_day;type:datetime;comment:调度-时间" json:"trigger_day"`
	RunningCount int       `gorm:"column:running_count;type:int(11);default:0;comment:运行中-日志数量;NOT NULL" json:"running_count"`
	SucCount     int       `gorm:"column:suc_count;type:int(11);default:0;comment:执行成功-日志数量;NOT NULL" json:"suc_count"`
	FailCount    int       `gorm:"column:fail_count;type:int(11);default:0;comment:执行失败-日志数量;NOT NULL" json:"fail_count"`
	UpdateTime   time.Time `gorm:"column:update_time;type:datetime" json:"update_time"`
}

func (m *XxlJobLogReport) TableName() string {
	return "xxl_job_log_report"
}

type XxlJobLogglue struct {
	Id         int       `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
	JobId      int       `gorm:"column:job_id;type:int(11);comment:任务，主键ID;NOT NULL" json:"job_id"`
	GlueType   string    `gorm:"column:glue_type;type:varchar(50);comment:GLUE类型" json:"glue_type"`
	GlueSource string    `gorm:"column:glue_source;type:mediumtext;comment:GLUE源代码" json:"glue_source"`
	GlueRemark string    `gorm:"column:glue_remark;type:varchar(128);comment:GLUE备注;NOT NULL" json:"glue_remark"`
	AddTime    time.Time `gorm:"column:add_time;type:datetime" json:"add_time"`
	UpdateTime time.Time `gorm:"column:update_time;type:datetime" json:"update_time"`
}

func (m *XxlJobLogglue) TableName() string {
	return "xxl_job_logglue"
}

type XxlJobRegistry struct {
	Id            int       `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
	RegistryGroup string    `gorm:"column:registry_group;type:varchar(50);NOT NULL" json:"registry_group"`
	RegistryKey   string    `gorm:"column:registry_key;type:varchar(255);NOT NULL" json:"registry_key"`
	RegistryValue string    `gorm:"column:registry_value;type:varchar(255);NOT NULL" json:"registry_value"`
	UpdateTime    time.Time `gorm:"column:update_time;type:datetime" json:"update_time"`
}

func (m *XxlJobRegistry) TableName() string {
	return "xxl_job_registry"
}

type XxlJobUser struct {
	Id         int    `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`
	Username   string `gorm:"column:username;type:varchar(50);comment:账号;NOT NULL" json:"username"`
	Password   string `gorm:"column:password;type:varchar(50);comment:密码;NOT NULL" json:"password"`
	Role       int    `gorm:"column:role;type:tinyint(4);comment:角色：0-普通用户、1-管理员;NOT NULL" json:"role"`
	Permission string `gorm:"column:permission;type:varchar(255);comment:权限：执行器ID列表，多个逗号分割" json:"permission"`
}

func (m *XxlJobUser) TableName() string {
	return "xxl_job_user"
}
